package com.intco.ait.quotation.api.starter.web;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.intco.ait.quotation.api.common.model.BaseResponse;
import com.intco.ait.quotation.api.common.model.request.MaterialSearchVo;
import com.intco.ait.quotation.api.infrastructure.mybatis.entity.encodingPlatform.Material;
import com.intco.ait.quotation.api.infrastructure.service.EncodingPlatformService;
import com.intco.ait.quotation.api.starter.advice.LocalException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/encoding-platform")
@Slf4j
public class EncodingPlatformController {

    @Autowired
    private EncodingPlatformService service;

    @PostMapping("/search")
    public BaseResponse<IPage<Material>> search(@RequestBody MaterialSearchVo search) {
        try {
            return BaseResponse.success(service.search(search));
        } catch (Exception e) {
            log.error("search调用失败", e);
            throw new LocalException(500, "查询失败", e);
        }
    }

    @GetMapping("/search/detail")
    public BaseResponse<Material> detail(String materialCode) {
        try {
            return BaseResponse.success(service.getProductAttributeByMaterialCode(materialCode));
        } catch (Exception e) {
            log.error("detail调用失败", e);
            throw new LocalException(500, "查询失败", e);
        }
    }
}
